package org.sunrise.game.game.modules;

import com.alibaba.fastjson2.TypeReference;
import lombok.Getter;
import lombok.Setter;
import org.sunrise.game.game.logic.ToolsUtils;

@Getter
@Setter
public class DataModule extends BaseModule {
    private String name;  //角色名字
    private int level;  //角色等级
    private int exp;  //经验
    private String headIcon;  //角色头像
    private int fightPower;  //战斗力
    private int sex;  //性别
    private long lastDailyRefreshTime; //上次跨天刷新的时间
    private long lastWeekRefreshTime; //上次跨周刷新的时间

    public DataModule(String humanId) {
        super(humanId);
    }

    @Override
    public void init() {
        name = "Human";
        headIcon = "default";
        sex = 1;
        lastDailyRefreshTime = ToolsUtils.getTodayZeroTimeMillis();
        lastWeekRefreshTime = ToolsUtils.getWeekZeroTimeMillis();
    }

    @Override
    public void load() {
        getDbData("name", new TypeReference<String>() {}, value -> name = value);
        getDbData("level", new TypeReference<Integer>() {}, value -> level = value);
        getDbData("exp", new TypeReference<Integer>() {}, value -> exp = value);
        getDbData("headIcon", new TypeReference<String>() {}, value -> headIcon = value);
        getDbData("fightPower", new TypeReference<Integer>() {}, value -> fightPower = value);
        getDbData("sex", new TypeReference<Integer>() {}, value -> sex = value);
        getDbData("ld", new TypeReference<Long>() {}, value -> lastDailyRefreshTime = value);
        getDbData("lw", new TypeReference<Long>() {}, value -> lastWeekRefreshTime = value);
    }

    @Override
    public void save() {
        putDbData("name", name);
        putDbData("level", level);
        putDbData("exp", exp);
        putDbData("headIcon", headIcon);
        putDbData("fightPower", fightPower);
        putDbData("sex", sex);
        putDbData("ld", lastDailyRefreshTime);
        putDbData("lw", lastWeekRefreshTime);
    }
}
